<?php
class EventModel extends Model {

    protected $fields=array(
        'id', 'owner', 'event', 'instance', 'created',
        '_pk'=>'id',
        '_autoInc'=>true
    );

    /**
     * 记录一次事件
     */
    public function logEvent($owner,$eventName,$eventInstance) {
        $eventVo=array(
            'owner'=>$owner,
            'event'=>$eventName,
            'instance'=>serialize($eventInstance),
            'created'=>date('Y-m-d H:i:s')
        );
        if($this->add($eventVo)) {
            return true;
        }
        return false;
    }

    /**
     * 查询最近的一次事件
     */
    public function findLastestEvent($owner,$eventName) {
        $rs=$this->field('instance')->limit('1')->order('created desc')->find();
        if($rs){
            return unserialize($rs['instance']);
        }
        return false;
    }

}
